/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.onurkagan.ksnack_lib.Animations;

import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.Component;

/**
 * 渐变动画
 *
 * @since 2021-07-01
 */
public class Fade {
    private static long DEFAULT_ANIM_TIME = 300;
    private static AnimatorProperty animation;

    /**
     * In
     *
     * @since 2021-07-08
     */
    public static class In {
        // Fade out anim without optional anim time and interpolator.

        /**
         * getAnimation
         *
         * @param lnrSnack lnrSnack
         * @return vs
         */
        public static AnimatorProperty getAnimation(Component lnrSnack) {
            animation = lnrSnack.createAnimatorProperty().alphaFrom(0).alpha(1);
            animation.setDuration(DEFAULT_ANIM_TIME);
            animation.setCurve(new Animator.TimelineCurve() {
                @Override
                public float getCurvedTime(float vs) {
                    return vs;
                }
            });
            return animation;
        }

        /**
         * getAnimation
         *
         * @param lnrSnack lnrSnack
         * @return vs
         */
        public static AnimatorProperty getAnimations(Component lnrSnack) {
            animation = lnrSnack.createAnimatorProperty().alphaFrom(0).alpha(1);
            animation.setDuration(DEFAULT_ANIM_TIME);
            animation.setCurve(new Animator.TimelineCurve() {
                @Override
                public float getCurvedTime(float vs) {
                    return vs;
                }
            });
            return animation;
        }

        // Fade out anim with optional anim time.

        /**
         * getAnimation
         *
         * @param lnrSnack lnrSnack
         * @param millisecond millisecond
         * @return vs
         */
        public static AnimatorProperty getAnimation(Component lnrSnack, long millisecond) {
            animation = lnrSnack.createAnimatorProperty().alphaFrom(0).alpha(1);
            animation.setDuration(millisecond);
            animation.setCurve(new Animator.TimelineCurve() {
                @Override
                public float getCurvedTime(float vs) {
                    return vs;
                }
            });
            return animation;
        }

        // Fade out anim with optional anim time and interpolator.

        /**
         * getAnimation
         *
         * @param lnrSnack lnrSnack
         * @param millisecond millisecond
         * @param interpolator interpolator
         * @return vs
         */
        public static AnimatorProperty getAnimation(Component lnrSnack,
            long millisecond, Animator.CurveType interpolator) {
            animation = lnrSnack.createAnimatorProperty().alphaFrom(0).alpha(1);
            animation.setDuration(millisecond);
            animation.setCurve(new Animator.TimelineCurve() {
                @Override
                public float getCurvedTime(float vs) {
                    return vs;
                }
            });
            return animation;
        }
    }

    /**
     * Out
     *
     * @since 2021-07-08
     */
    public static class Out {
        // Fade out anim without optional anim time and interpolator.

        /**
         * getAnimation
         *
         * @param lnrSnack lnrSnack
         * @return vs
         */
        public static AnimatorProperty getAnimation(Component lnrSnack) {
            animation = lnrSnack.createAnimatorProperty().alphaFrom(1).alpha(0);
            animation.setDuration(DEFAULT_ANIM_TIME);
            animation.setCurve(new Animator.TimelineCurve() {
                @Override
                public float getCurvedTime(float vs) {
                    return vs;
                }
            });
            return animation;
        }

        /**
         * getAnimation
         *
         * @param lnrSnack lnrSnack
         * @return vs
         */
        public static AnimatorProperty getAnimations(Component lnrSnack) {
            animation = lnrSnack.createAnimatorProperty().alphaFrom(1).alpha(0);
            animation.setDuration(DEFAULT_ANIM_TIME);
            animation.setCurve(new Animator.TimelineCurve() {
                @Override
                public float getCurvedTime(float vs) {
                    return vs;
                }
            });
            return animation;
        }

        // Fade out anim with optional anim time.

        /**
         * getAnimation
         *
         * @param lnrSnack lnrSnack
         * @param millisecond millisecond
         * @return vs
         */
        public static AnimatorProperty getAnimation(Component lnrSnack, int millisecond) {
            animation = lnrSnack.createAnimatorProperty().alphaFrom(0).alpha(1);
            animation.setDuration(millisecond);
            animation.setCurve(new Animator.TimelineCurve() {
                @Override
                public float getCurvedTime(float vs) {
                    return vs;
                }
            });
            return animation;
        }

        // Fade out anim with optional anim time and interpolator.

        /**
         * getAnimation
         *
         * @param lnrSnack lnrSnack
         * @param millisecond millisecond
         * @param interpolator interpolator
         * @return vs
         */
        public static AnimatorProperty getAnimation(Component lnrSnack,
            int millisecond, Animator.CurveType interpolator) {
            animation = lnrSnack.createAnimatorProperty().alphaFrom(1).alpha(0);
            animation.setDuration(millisecond);
            animation.setCurve(new Animator.TimelineCurve() {
                @Override
                public float getCurvedTime(float vs) {
                    return vs;
                }
            });
            return animation;
        }
    }
}
